package control

import (
	"WeChatMGRA/requests"
	"WeChatMGRA/server/conf"
	"fmt"
	"gitee.com/yutiandou/wdtools/wdlog"
	"github.com/buaazp/fasthttprouter"
	"github.com/valyala/fasthttp"
)

var ReceiveMsgList  chan []byte

//StartReceiveServer ..开启信息接收监听服务
func StartReceiveServer(config * conf.Config){
	go func() {
		//初始化消息管道
		//ReceiveMsgList = make(chan []byte,config.MaxReceiveMsgCount)
		//StartWorkPool()
		//初始化服务器
		monserver := fasthttprouter.New()
		monserver.POST("/wxzsReceive",Receivehandler)
		monserver.GET("/test",func(ctx * fasthttp.RequestCtx){
			ctx.Write([]byte("hello world"))
		})
		if err:= fasthttp.ListenAndServe(fmt.Sprintf("0.0.0.0:%s",config.WKListenPort), monserver.Handler);wdlog.PanicErr(err){
		}
	}()
	//设置消息接收地址
    respdata,err:=requests.MakeARequest("setHttpCallbackUrl",[]byte(fmt.Sprintf("{\"httpUrl\":\"http://%s:%s/wxzsReceive\"}",config.WKListenAddr,config.WKListenPort)))
	//respdata,err:=requests.MakeARequest("setHttpCallbackUrl",[]byte("{\"httpUrl\": \"http://zdsh.in.8866.org:38485/wxzsReceive\"}"))

	if wdlog.PanicErr(err){
	}
	msg,err:=requests.VerifyWKResp(respdata,nil)
	if wdlog.PanicErr(err){
	}
	if msg.Code=="1000"{
		wdlog.Debugln("接收信息地址配置成功")
	}else{
		wdlog.Panicln ("接收信息地址配置失败")
	}

}


//Receivehandler 接收信息回调函数
func Receivehandler(ctx *fasthttp.RequestCtx){
	data:=ctx.PostBody()
	Msg_deal_worker(data)
	//modul.GiveRecvinfo(msg)
}
